import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Widget/MyButton.dart';
import 'package:hitutor/Widget/MyImage.dart';

class NormalInput extends StatelessWidget {
  static const String Number = "number";
  static const String Password = "password";
  static const String Email = "email";
  static const String Phone = "phone";
  static const String Account = "account";
  List<TextInputFormatter>? numberFormatter = [FilteringTextInputFormatter.digitsOnly];
  List<TextInputFormatter>? phoneFormatter = [FilteringTextInputFormatter.allow(RegExp("[0-9-]"))];
  List<TextInputFormatter>? accountFormatter = [FilteringTextInputFormatter.allow(RegExp("[a-zA-Z]|[0-9-]"))];
  List<TextInputFormatter>? passwordFormatter = [FilteringTextInputFormatter.allow(RegExp("[a-zA-Z]|[0-9.]"))];
  // List<TextInputFormatter>? emailFormatter = [FilteringTextInputFormatter.allow(RegExp("[a-zA-Z]|[0-9.@]"))];

  List<TextInputFormatter>? inputFormatters;
  TextInputType? keyboardType;
  bool isPassword = false;
  String? hintText;
  TextEditingController? controller;
  TextAlign? textAlign;
  double? fontSize;
  String? inputType;
  FocusNode? focusNode;
  int? maxLines;
  bool? enable;
  Color? hintColor;
  Color? fontColor;
  Function(String value)? onSubmit;
  Function(String value)? onChanged;

  NormalInput(
      {this.controller,
      this.hintColor,
      this.fontColor,
      this.hintText,
      this.enable,
      this.textAlign,
      this.fontSize,
      this.onSubmit,
      this.onChanged,
      this.inputType,
      this.focusNode,
      this.maxLines}) {
    isPassword = false;
    if (inputType == Number) {
      keyboardType = TextInputType.number;
      inputFormatters = numberFormatter;
    } else if (inputType == Phone) {
      keyboardType = TextInputType.number;
      inputFormatters = phoneFormatter;
    } else if (inputType == Account) {
      keyboardType = TextInputType.number;
      inputFormatters = accountFormatter;
    } else if (inputType == Password) {
      isPassword = true;
      keyboardType = TextInputType.visiblePassword;
      inputFormatters = passwordFormatter;
    } else if (inputType == Email) {
      keyboardType = TextInputType.emailAddress;
      // inputFormatters = emailFormatter;
    }
    if (maxLines == 0) {
      maxLines = null;
    } else {
      maxLines = 1;
    }
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: controller,
      keyboardType: keyboardType,
      maxLines: maxLines,
      focusNode: focusNode,
      enabled: enable,
      onSubmitted:onSubmit ,
      onChanged: onChanged,
      decoration: InputDecoration(
        hintText: hintText,
        contentPadding: EdgeInsets.all(0),
        hintStyle: TextStyle(color: hintColor??MyColor.A0, fontSize: fontSize ?? 14),
        enabledBorder: OutlineInputBorder(borderSide: BorderSide(color: MyColor.Transfer, width: 0)),
        disabledBorder: OutlineInputBorder(borderSide: BorderSide(color: MyColor.Transfer, width: 0)),
        focusedBorder: OutlineInputBorder(borderSide: BorderSide(color: MyColor.Transfer, width: 0)),
      ),
      style: TextStyle(color: fontColor??MyColor.Black, fontSize: fontSize ?? 14),
      cursorColor: MyColor.BlueLow,
      obscureText: isPassword,
      // cursorHeight: 18,
      inputFormatters: inputFormatters,
      textAlign: textAlign ?? TextAlign.start,
      // textAlignVertical: TextAlignVertical.center,
    );
  }
}

class TitleInput extends StatelessWidget {
  String title;
  NormalInput input;
  EdgeInsetsGeometry? margin;
  Widget? rightBtn;

  TitleInput(this.title, this.input, {this.margin, this.rightBtn});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 44,
      margin: margin,
      padding: EdgeInsets.fromLTRB(18, 0, 18, 0),
      decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(8)), border: Border.all(width: 1.2, color: MyColor.EE)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Padding(padding: EdgeInsets.only(bottom: 2), child: Text(title, style: TextStyle(fontSize: 14, color: MyColor.Black))),
          Container(width: 10),
          Expanded(child: input),
          Container(child: rightBtn)
        ],
      ),
    );
  }
}

class TitleChooser extends StatelessWidget {
  String title;
  String hintText;
  String content;
  EdgeInsetsGeometry? margin;
  Function()? onTap;

  TitleChooser(this.title, this.hintText, this.content, {this.margin, this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: onTap,
        child: Container(
          width: double.infinity,
          height: 44,
          margin: margin,
          padding: EdgeInsets.fromLTRB(18, 0, 18, 0),
          decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(8)), border: Border.all(width: 1.2, color: MyColor.EE)),
          child: Row(
            children: [
              Text(title, style: TextStyle(fontSize: 14, color: MyColor.Black)),
              Container(width: 10),
              Expanded(
                  child: Text(content == "" ? hintText : content,
                      style: TextStyle(fontSize: 14, color: content == "" ? MyColor.A0 : MyColor.Black))),
              LocalImage("ic_arrow_down.png", width: 11)
            ],
          ),
        ));
  }
}

class IconInput extends StatelessWidget {
  IconInput(this.iconPath, this.title,
      {this.hintText, this.controller, this.margin, this.isBorder, this.onTap, this.btnText, this.inputType});

  String? btnText;
  void Function()? onTap;
  String iconPath;
  String title;
  String? hintText;
  EdgeInsetsGeometry? margin;
  TextEditingController? controller = TextEditingController();
  bool? isBorder = false;
  String? inputType;

  @override
  Widget build(BuildContext context) {
    isBorder = isBorder ?? false;
    return Container(
      height: 50,
      margin: margin,
      decoration: isBorder!
          ? BoxDecoration(
              color: MyColor.White, borderRadius: BorderRadius.all(Radius.circular(25)), border: Border.all(width: 1.2, color: MyColor.E9))
          : BoxDecoration(color: MyColor.White, borderRadius: BorderRadius.all(Radius.circular(8))),
      child: Row(
        children: [
          LocalImage(iconPath, width: 24, height: 24, padding: EdgeInsets.fromLTRB(15, 0, 5, 0)),
          Container(width: 70, child: Text(title, style: TextStyle(fontSize: 14, color: MyColor.Black))),
          Expanded(child: NormalInput(hintText: hintText, controller: controller, inputType: inputType)),
          Visibility(child: Button(btnText ?? "", onTap: onTap, width: 80, height: 34, fontSize: 10), visible: btnText != null)
        ],
      ),
    );
  }
}

class StarLayout extends StatelessWidget {
  Widget child;

  StarLayout(this.child);

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [Text("*", style: TextStyle(color: MyColor.Red, fontSize: 15)), Expanded(child: child)],
    );
  }
}

class Input extends StatelessWidget {
  String title;
  NormalInput input;

  Input(this.title, this.input);

  @override
  Widget build(BuildContext context) {
    return Container(
        width: double.infinity,
        height: 58,
        padding: EdgeInsets.fromLTRB(0, 0, 18, 0),
        child: Row(crossAxisAlignment: CrossAxisAlignment.center, children: [
          Padding(
              padding: EdgeInsets.only(bottom: 2),
              child: Text(title, style: TextStyle(fontSize: 15, color: MyColor.Black, fontWeight: FontWeight.bold))),
          // Container(width: 10),
          Expanded(child: input),
        ]));
  }
}
